﻿/*
 * ImageGallery 
 * An ASP.NET control for publishing Image Galleries on the web.
 * Copyright (C) 2008 Imesh Gunaratne
 * http://code.google.com/p/imagegallery
 * 
 * This file is part of ImageGallery.
 *
 * ImageGallery is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ImageGallery is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with ImageGallery.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * History: 2008/10/07 Created.
 *          
 */

using System;
using System.Collections.Generic;
using FlickrNet;

namespace ImageGallery
{
    public static class FlickrCache
    {
        #region Private Attributes
        private static Flickr flickr;
        private static PhotoCollection photoCollection;
        private static List<KeyValuePair<string, string>> imageDescriptions;
        #endregion

        #region Public Properties
        public static List<KeyValuePair<string, string>> ImageDescriptions
        {
            get { return imageDescriptions; }
            set { imageDescriptions = value; }
        }

        public static PhotoCollection PhotoCollection
        {
            get { return photoCollection; }
            set { photoCollection = value; }
        }

        public static int Count
        {
            get
            {
                if (photoCollection != null)
                    return photoCollection.Count;
                else
                    return 0;
            }
        }
        #endregion

        #region Private Methods
        private static void initializeFlickr()
        {
            if (flickr == null)
            {
                string flickrApiKey = DefaultSettings.FlickrApiKey;
                if (flickrApiKey != null)
                    flickr = new Flickr(flickrApiKey);
            }
        }

        private static string GetImageDescription(Photo image)
        {
            try
            {
                initializeFlickr();

                PhotoInfo photoInfo = flickr.PhotosGetInfo(image.PhotoId);
                return photoInfo.Description;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return null;
        }
        #endregion

        #region Public Methods
        public static void UpdateFlickrImageCache()
        {
            initializeFlickr();
            if (flickr != null)
            {
                PhotoSearchOptions options = new PhotoSearchOptions();
                options.UserId = DefaultSettings.FlickrUserID;
                options.PerPage = 1000;
                options.Page = 1;

                Photos photos = flickr.PhotosSearch(options);
                PhotoCollection = photos.PhotoCollection;

                if (PhotoCollection != null)
                {
                    ImageDescriptions = new List<KeyValuePair<string, string>>();
                    foreach (Photo photo in PhotoCollection)
                    {
                        KeyValuePair<string, string> pair = new KeyValuePair<string, string>(photo.PhotoId, GetImageDescription(photo));
                        ImageDescriptions.Add(pair);
                    }
                }
            }
        }

        public static string GetDescription(string photoId)
        {
            if(ImageGallery.FlickrCache.ImageDescriptions != null)
            {
                foreach (KeyValuePair<string, string> pair in ImageGallery.FlickrCache.ImageDescriptions)
                {
                    if ((pair.Key != null) && (pair.Key.Equals(photoId)))
                        return pair.Value;
                }
            }
            return null;
        }
        #endregion
    }
}
